import Vue from "vue";
import VueRouter from "vue-router";
import Home from "./views/Home.vue";
import store from './store';
import { MessageBox } from 'element-ui';

Vue.use(VueRouter);

const routes = [{
  path: "/",
  name: "home",
  component: Home
}, {
  path: "/login/:type",
  name: "login",
  component: () => import(`./views/LoginRegist.vue`),
  meta: {
    title: "登录",
  }
}, {
  path: "/regist",
  name: "regist",
  component: () => import(`./views/LoginRegist.vue`),
  meta: {
    title: "注册",
  }
}, {
  path: "/personalCenter/:tabName?",
  alias: '/personal_center/:tabName?',   //别名
  name: "personalCenter",
  component: () => import("./views/PersonalCenter.vue"),
  meta: {
    title: "个人中心",
    requireAuth: true
  }
}, {
  path: "/search",
  name: "search",
  component: () => import("./views/Search.vue"),
  meta: {
    title: "搜索",
  }
}, {
  path: "/brandZone/:id",
  alias: "/brand_zone/:id",
  name: "brandZone",
  component: () => import("./views/BrandZone.vue"),
  meta: {
    title: "品牌专区",
  }
}, {
  path: "/goodsDetail/:id",
  alias: "/goods_detail/:id",
  name: "goodsDetail",
  component: () => import("./views/GoodsDetail.vue"),
  meta: {
    title: "商品详情",
  }
}, {
  path: "/confirmOrder",
  alias: "/confirm_order",
  name: "confirmOrder",
  component: () => import("./views/ConfirmOrder.vue"),
  meta: {
    title: "确认订单",
    requireAuth: true
  }
}, {
  path: "/foundPassword",
  alias: "/found_password",
  name: "foundPassword",
  component: () => import("./views/FoundPassword.vue"),
  meta: {
    title: "找回密码",
  }
}, {
  path: "/registProtocol",
  alias: "/regist_protocol",
  name: "registProtocol",
  component: () => import("./views/RegistProtocol.vue"),
  meta: {
    title: "注册协议",
  }
}, {
  path: '/notFound',
  alias: "/not_found",
  name: "notFound",
  component: () => import(`./views/404.vue`),
  meta: {
    title: "404",
  }
}, {
  path: '*',
  redirect: '/notFound'
}];


const router = new VueRouter({
  routes,
  // mode: "history",  //可以去除地址栏的#；使用history模式需要后端的配合，如果没有后端的配合在打包的时候页面会是空白的。一般还是使用hash模式
  // base: process.env.BASE_URL,
  linkExactActiveClass: "active",
});
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth && !store.state.hasLogin) {
    MessageBox.confirm(`前往${to.meta.title}页面需要登录，是否立即登录？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      next("/login/psd");
    }).catch(() => { });
  } else {
    document.title = to.meta.title ? to.meta.title : "首页";
    next();
  }
})
export default router;
